﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using QskjLibrary;

namespace QskjServiceKafka.Service
{
    internal class CarDriverInfo
    {

        public static void ContactDriverInfo()
        {
            try
            {
                var redisIn = RedisHelp.GetInstance1();

                string sql = "select count(1) from qskjrecord.B_TaxiCarDriverZT" + DateTime.Now.ToString("yyyyMMdd") + " a inner join  qskjrecord.B_TaxiDriverZT" + DateTime.Now.ToString("yyyyMMdd") + " b on a.permit_number=b.permit_number  inner join qskjrecord.B_TaxiZT" + DateTime.Now.ToString("yyyyMMdd") + " c on a.car_numbers = c.car_num";
                DataTable czcData1 = QskjLibrary.MySqlHelper.GetDataTable(System.Data.CommandType.Text, sql);

                int count = Convert.ToInt32(czcData1.Rows[0][0]);
                int page = (count / 1000) + 1;
                for (int i = 0; i < page; i++)
                {

                    sql = "select c.car_numbers,b.name,b.head_url,b.id_card from qskjrecord.B_TaxiCarDriverZT" + DateTime.Now.ToString("yyyyMMdd") + " a inner join  qskjrecord.B_TaxiDriverZT" + DateTime.Now.ToString("yyyyMMdd") + " b on a.permit_number=b.permit_number  inner join qskjrecord.B_TaxiZT" + DateTime.Now.ToString("yyyyMMdd") + " c on a.car_numbers = c.car_num  LIMIT " + 1000 * i + ", 1000";
                    DataTable czcData = QskjLibrary.MySqlHelper.GetDataTable(System.Data.CommandType.Text, sql);

                    foreach (DataRow row in czcData.Rows)
                    {
                        string s = row["car_numbers"] == DBNull.Value ? "" : row["car_numbers"].ToString();
                        if (!string.IsNullOrWhiteSpace(s))
                        {
                            redisIn.Set("driver_" + Convert.ToString(row["car_numbers"]), string.Format("{0}_{1}_{2}", Convert.ToString(row["name"]), Convert.ToString(row["head_url"]), Convert.ToString(row["id_card"])));

                        }
                    }
                }
                sql = "select a.license_plate_number_hudi,b.name,b.head_url,b.id_card from qskjrecord.B_BusLeaseRecordZT" + DateTime.Now.ToString("yyyyMMdd") + " a inner join qskjrecord.B_TaxiDriverZT" + DateTime.Now.ToString("yyyyMMdd") + " b on a.id_number_hudi=b.id_card";
                QskjLibrary.MySqlHelper.GetDataTable(System.Data.CommandType.Text, sql);
                DataTable dbcData = QskjLibrary.MySqlHelper.GetDataTable(System.Data.CommandType.Text, sql);

                foreach (DataRow row in dbcData.Rows)
                {
                    string s = row["car_numbers"] == DBNull.Value ? "" : row["car_numbers"].ToString();
                    if (!string.IsNullOrWhiteSpace(s))
                    {
                        redisIn.Set("driver_" + Convert.ToString(row["car_numbers"]), string.Format("{0}_{1}_{2}", Convert.ToString(row["name"]), Convert.ToString(row["head_url"]), Convert.ToString(row["id_card"])));

                    }
                }

                QskjLibrary.MySqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, sql);
                sql = "INSERT INTO qskj.S_LogRecords(MachineName, Application,CreateDateTime, Thread, `LogLevel`, `Logger`, `Message`, `Exception`)VALUES('驾驶员信息定时任务','驾驶员信息定时任务','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "', '驾驶员信息定时任务','驾驶员信息定时任务', '驾驶员信息定时任务', '驾驶员信息定时任务', '驾驶员信息定时任务');";
                QskjLibrary.MySqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                try
                {
                    Thread.Sleep(1000);
                    string sql = "INSERT INTO qskj.S_LogRecords(MachineName, Application,CreateDateTime, Thread, `LogLevel`, `Logger`, `Message`, `Exception`)VALUES('驾驶员信息定时任务','驾驶员信息定时任务失败','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "', '驾驶员信息定时任务','驾驶员信息定时任务', '驾驶员信息定时任务', '驾驶员信息定时任务', '" + ex.Message + "');";
                    QskjLibrary.MySqlHelper.ExecuteNonQuery(System.Data.CommandType.Text, sql);
                    Console.WriteLine("读取出租车信息异常" + ex.Message);
                }
                catch (Exception ex1)
                {
                    Console.WriteLine("读取出租车信息异常" + ex1.Message);
                }

            }

        }
    }
}
